﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;
using System.IO;
using Avatar.Common;

public partial class Management_ENewsPaperConfig : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                if (FileUpload1.HasFile)
                {
                    var _config = _adc.ENewsPaperConfig.FirstOrDefault();
                    if (_config == null)
                    {
                        _config = new ENewsPaperConfig();

                        #region 入口检测

                        if (!FileUpload1.HasFile) throw new Exception("图片内容不能为空");
                        var _fileOK = false;
                        String _fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
                        String[] _allowedExtensions = { ".gif", ".jpeg", ".jpg" };
                        foreach (var _e in _allowedExtensions)
                        {
                            if (_fileExtension.ToLower() == _e.ToLower())
                            {
                                _fileOK = true;
                            }
                        }
                        if (_fileOK != true) throw new Exception("*文件类型错误：只能上传是扩展名为:gif、jpeg、jpg的文件.");

                        #endregion

                        #region 图片上传

                        var _year = DateTime.Now.Year.ToString();
                        var _month = DateTime.Today.Month.ToString();
                        var _day = DateTime.Today.Day.ToString();
                        var _hour = DateTime.Now.Hour.ToString();
                        var _minute = DateTime.Now.Minute.ToString();
                        var _second = DateTime.Now.Second.ToString();

                        string _path = "~/ad/" + _year + "/" + _month + "/" + _day + "/" + _hour + "/";
                        String _savePath = Server.MapPath("~/ad/") + _year + "\\" + _month + "\\" + _day + "\\" + _hour + "\\";
                        Directory.CreateDirectory(_savePath);

                        string _fileName = Guid.NewGuid().ToString() + _fileExtension;

                        string _iconURL = _savePath + _fileName;


                        FileUpload1.PostedFile.SaveAs(_iconURL);

                        _config.Icon = _path + _fileName; ;
                        #endregion

                        _adc.ENewsPaperConfig.InsertOnSubmit(_config);
                    }
                    else
                    {
                        #region 图片上传

                        #region 入口检测

                        if (FileUpload1.HasFile)
                        {
                            var _fileOK = false;
                            String _fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
                            String[] _allowedExtensions = { ".gif", ".jpeg", ".jpg" };
                            foreach (var _e in _allowedExtensions)
                            {
                                if (_fileExtension.ToLower() == _e.ToLower())
                                {
                                    _fileOK = true;
                                }
                            }
                            if (_fileOK != true) throw new Exception("*文件类型错误：只能上传是扩展名为:gif、jpeg、jpg的文件.");
                        }
                        #endregion

                        String _savePath = Server.MapPath(_config.Icon.Substring(0, _config.Icon.LastIndexOf('/')) + "\\");

                        string _fileName = _config.Icon.Substring(_config.Icon.LastIndexOf('/') + 1);

                        FileUpload1.PostedFile.SaveAs(_savePath + _fileName);

                        #endregion
                    }

                    _adc.SubmitChanges();
                    Helper.ShowMassage("修改成功！");
                }
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "错误:" + _e.Message;
        }
    }
}
